<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	set_html('title','在线支付');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$qrid=$postdata['qrid'];
		$qr=pdo_get('jy_cashier_qrcode',array('id'=>$qrid));
		if($qr['money']<=0){
			if($postdata['paymoney']<=0){
				exi('请输入支付金额！','error');
			}
			$qr['money']=$postdata['paymoney'];
		}
		$res=$this->model('model')->upfee($qr['money'],$qr['cashierid']);
		$qr['money']=$res['lastfee'];
		$c=pdo_get('jy_cashier_user',array('id'=>$qr['cashierid']));
		$TIME=time();
		$tid='CS'.$TIME.rand(10000,99999);
		//$data=array('orderid'=>$tid,'uniacid'=>$_SESSION['uniacid'],'cashierid'=>$qr['cashierid'],'money'=>$qr['money'],'paytime'=>time(),'createtime'=>time(),'paytype'=>1,'payopenid'=>$_SESSION['openid'],'status'=>1);
		//pdo_insert('jy_cashier_pay_log',$data); 
		$data=array(
			'ordersn'=>$tid,
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'cashierid'=>$qr['cashierid'],
			'price'=>$qr['money'],
			'createtime'=>time(),
			'status'=>0,
			'payopenid'=>$_SESSION['openid']
		);
		pdo_insert('jy_cashier_order',$data); 
		$oid=pdo_insertid();	
		$title=$qr['goodstitle']?$qr['goodstitle']:$c['title'];
		$params=array('title'=>$title,'fee'=>$qr['money'],'id'=>$oid,'plugin'=>'jy_cashier');	
		$params['back_url']='jy_cashier/pay/payresult';
		$return['payid']=cfc('pay')->create_pay_order($params);
		exi($return);	
	}
	$return['qrid']=$qrid=$_RQ['qrid'];
	$qr=pdo_get('jy_cashier_qrcode',array('id'=>$qrid));
	$c=pdo_get('jy_cashier_user',array('id'=>$qr['cashierid']));
	$_SESSION['cashierid']=$qr['cashierid'];
	$title=$qr['goodstitle']?$qr['goodstitle']:$c['title'];
	$return['c']=$c;
	$return['title']=$title;
	$return['money']=$qr['money'];
	if(!$qr){
		exi('参数错误','error');
	}
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('收款失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('收款失败！','error');
	}
	$order=pdo_get('jy_cashier_order',array('id'=>$payresult['id']));
	if(!$order){
		exi('收款失败！订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('收款失败！订单金额不匹配！','error');
	}
	
	pdo_update('jy_cashier_order',array('status'=>1,'paytime'=>time()),array('id'=>$order['id']));
	$da=array(
		'orderid'=>$order['id'],
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$order['mid'],
		'cashierid'=>$order['cashierid'],
		'createtime'=>time(),
		'money'=>$order['price'],
		'paytype'=>1,
		'paytime'=>time(),
		'payopenid'=>$order['openid']
	);
	pdo_insert('jy_cashier_pay_log',$da); 
	exi('收款成功','success');
}
if($_RQ['op']=='upfee'){
	 $money=$_RQ['money'];
	 if($money<=0){
		 exi('请输入正确金额','error');
	 }
	$return=$this->model('model')->upfee($money,$_SESSION['cashierid']);
	exi($return);
}
